package com.citcc.api.netty.utils;


public class XcUtil {
	public static String convertStringToHex(String str){
		 
		  char[] chars = str.toCharArray();
	 
		  StringBuffer hex = new StringBuffer();
		  for(int i = 0; i < chars.length; i++){
		    hex.append(Integer.toHexString((int)chars[i]));
		  }
	 
		  return hex.toString();
		  }
	//16进制转10进制字符创
	public static String convertHexToString(String hex){
		 
		  StringBuilder sb = new StringBuilder();
		  StringBuilder temp = new StringBuilder();
	 
		  //49204c6f7665204a617661 split into two characters 49, 20, 4c...
		  for( int i=0; i<hex.length()-1; i+=2 ){
	 
		      //grab the hex in pairs
		      String output = hex.substring(i, (i + 2));
		      //convert hex to decimal
		      int decimal = Integer.parseInt(output, 16);
		      //convert the decimal to character
		      sb.append((char)decimal);
	 
		      temp.append(decimal);
		  }
		  
		  return sb.toString();
	  }
	
	
	public static String stringToAscii(String val) {

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < val.length(); i++) {
            char c = val.charAt(i);
            builder.append((byte) c).append(",");
        }

        return builder.toString();
    }

	public static String asciiToString(String value)  
    {  
        StringBuffer stringBuffer = new StringBuffer();  
        String[] chars = value.split(",");  
        for(String c : chars){
            stringBuffer.append((char) Integer.parseInt(c,16));  
        }
        return stringBuffer.toString();  
    }  
	
	
	/*public static void test(){
		Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        buffer.writeByte((byte) cal.get(Calendar.DATE));
        buffer.writeByte((byte) (cal.get(Calendar.MONTH) + 1));
        String hexYear = "0" + Integer.toHexString(cal.get(Calendar.YEAR));
        buffer.writeBytes(hexStringToByte(hexYear));
        //时分秒
        buffer.writeByte((byte) cal.get(Calendar.HOUR_OF_DAY));
        buffer.writeByte((byte) cal.get(Calendar.MINUTE));
        buffer.writeByte((byte) cal.get(Calendar.SECOND));
	}*/

	
	public static void main(String[] args) {
		 
		  String s = "32303139303130303030303458433031";
		  System.out.println("\n-----ASCII码转换为16进制 -----");
		  String str = "02302A155HS00001"; 
		  System.out.println("字符串: " + str);
		  String hex = XcUtil.convertStringToHex(str);
		  System.out.println("转换为16进制 : " + hex);
	 
		  System.out.println("\n***** 16进制转换为ASCII *****");
		  System.out.println("Hex : " + hex);
		  System.out.println("ASCII : " + XcUtil.convertHexToString("32303139303130303030303458433031"));
		/*Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        System.out.println("ri==="+cal.get(Calendar.DATE));
        int yue = cal.get(Calendar.MONTH) + 1;
        System.out.println("yue==="+yue);
        System.out.println("nian===="+cal.get(Calendar.YEAR));
        //时分秒
        System.out.println("h==="+cal.get(Calendar.HOUR_OF_DAY));
        System.out.println("m==="+cal.get(Calendar.MINUTE));
        System.out.println("s==="+cal.get(Calendar.SECOND));
        System.out.println("ss==="+cal.get(Calendar.MILLISECOND));
        
        double l = 113933967;
        System.out.println("========="+(l/1000000));*/
	}


}
